# PowerShell script for deploying to Windows Azure

$cert = Get-Item cert:\CurrentUser\My\9588271A0108FFFC62599C103CB2A501294FFB8C
 
$sub = "360e4de8-ae55-42b6-8177-5e3f40635940"
$buildPath = "."
$packageBase = $args[0]
$packageName = $packageBase + ".cspkg"
$servicename = $args[1]
$buildNumber = $args[2]

$serviceconfig = "ServiceConfiguration.cscfg"

$storageservicename = "activedeck"
$package = join-path $buildPath $packageName
$config = join-path $buildPath $serviceconfig
$a = Get-Date
$buildLabel = $packageBase + " - " + $buildNumber

write-host Deploying: $buildLabel

if ((Get-PSSnapin | ?{$_.Name -eq "WAPPSCmdlets"}) -eq $null)
{
  Add-PSSnapin WAPPSCmdlets 
}
 
write-host Getting service...
$hostedService = Get-HostedService $servicename -Certificate $cert -SubscriptionId $sub | Get-Deployment -Slot Staging
 
if ($hostedService.Status -ne $null)
{
    write-host Service found.
 
    write-host Suspending service...
    $hostedService |
      Set-DeploymentStatus 'Suspended' |
      Get-OperationStatus -WaitToComplete
 
    write-host Removing deployment...
    $hostedService |
      Remove-Deployment |
      Get-OperationStatus -WaitToComplete
}
 
write-host Creating staging deployment...
Get-HostedService $servicename -Certificate $cert -SubscriptionId $sub |
    New-Deployment Staging -package $package -configuration $config -label $buildLabel -StorageServiceName $storageservicename |
    Get-OperationStatus -WaitToComplete
 
write-host Starting staging deployment...
Get-HostedService $servicename -Certificate $cert -SubscriptionId $sub |
    Get-Deployment -Slot Staging |
    Set-DeploymentStatus 'Running' |
    Get-OperationStatus -WaitToComplete

write-host Verifying service...
$hostedService = Get-HostedService $servicename -Certificate $cert -SubscriptionId $sub | Get-Deployment -Slot Staging
 
if ($hostedService.Status -ne "Running")
{
    write-host Service not running
    exit 1
}

write-host Done!